package com.adee.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.StandardCharsets;

/*
* 说明
* 继承一个HandlerAdapter: ChannelInboundHandlerAdapter
* */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 读取从客户端发送来的StudentPOJO.Student对象
//        StudentPOJO.Student stu = (StudentPOJO.Student)msg;
//        System.out.println("接收到客户端的数据：id=" + stu.getId() + ",name=" + stu.getName());
//        System.out.println(msg.getClass());
        ByteBuf buf = (ByteBuf)msg;
        System.out.println(new String(buf.array(), StandardCharsets.UTF_8));
    }
    /* 数据读取完毕 */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

    }
    /* 处理异常，一般是关闭通道 */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

}
